#include ".\cardconfigview.h"
#include <QVBoxLayout>
#include <QScrollArea>
#include <QFrame>
#include <QLabel>
#include <QGridLayout>
#include <QJsonObject>
#include <QJsonArray>
#include <QSpacerItem>

CardConfigView::CardConfigView(QObject *parent)
    : ConfigView(parent)
{
    m_view = new QWidget();
    QVBoxLayout *mainLayout = new QVBoxLayout(m_view);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 创建滚动区域
    m_scrollArea = new QScrollArea();
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setFrameShape(QFrame::NoFrame);

    m_scrollContent = new QWidget();
    QVBoxLayout *contentLayout = new QVBoxLayout(m_scrollContent);
    contentLayout->setAlignment(Qt::AlignTop);

    m_scrollArea->setWidget(m_scrollContent);
    mainLayout->addWidget(m_scrollArea);
}

CardConfigView::~CardConfigView()
{
    if (m_view) {
        delete m_view;
    }
}

QWidget* CardConfigView::viewWidget()
{
    return m_view;
}

void CardConfigView::updateConfig(const QJsonArray &config)
{
    m_currentConfig = config;

    // 清除旧内容
    if (!m_scrollContent) return;

    QLayout *contentLayout = m_scrollContent->layout();
    if (contentLayout) {
        QLayoutItem *child;
        while ((child = contentLayout->takeAt(0)) != nullptr) {
            delete child->widget();
            delete child;
        }
    } else {
        contentLayout = new QVBoxLayout(m_scrollContent);
        contentLayout->setAlignment(Qt::AlignTop);
    }

    // 创建卡片视图
    for (const QJsonValue &configVal : m_currentConfig) {
        QJsonObject configObj = configVal.toObject();
        QString type = configObj["type"].toString();
        QString title = type.toUpper() + " 配置";

        // 创建卡片容器
        QFrame *card = new QFrame();
        card->setFrameShape(QFrame::StyledPanel);
        card->setStyleSheet(
            "QFrame {"
            "   background-color: white;"
            "   border-radius: 8px;"
            "   padding: 15px;"
            "   margin: 10px;"
            "   box-shadow: 0 2px 5px rgba(0,0,0,0.1);"
            "}"
        );

        QVBoxLayout *cardLayout = new QVBoxLayout(card);

        // 卡片标题
        QLabel *titleLabel = new QLabel(title);
        titleLabel->setStyleSheet(
            "QLabel {"
            "   font-weight: bold;"
            "   font-size: 14pt;"
            "   color: #2c3e50;"
            "   padding-bottom: 10px;"
            "   border-bottom: 1px solid #ecf0f1;"
            "}"
        );
        cardLayout->addWidget(titleLabel);

        // 卡片内容
        QWidget *contentWidget = new QWidget();
        QGridLayout *gridLayout = new QGridLayout(contentWidget);
        gridLayout->setColumnStretch(1, 1); // 第二列拉伸

        int row = 0;

        if (configObj.contains("config")) {
            QJsonValue configValue = configObj["config"];

            if (type == "io") {
                QJsonArray ioArray = configValue.toArray();
                for (const QJsonValue &ioVal : ioArray) {
                    QJsonObject ioObj = ioVal.toObject();

                    gridLayout->addWidget(new QLabel("<b>引脚:</b>"), row, 0);
                    gridLayout->addWidget(new QLabel(QString::number(ioObj["io_pin"].toInt())), row, 1);

                    gridLayout->addWidget(new QLabel("<b>模式:</b>"), row, 2);
                    gridLayout->addWidget(new QLabel(ioObj["mode"].toString()), row, 3);

                    if (ioObj.contains("active_state")) {
                        gridLayout->addWidget(new QLabel("<b>激活状态:</b>"), row, 4);

                        QLabel *stateLabel = new QLabel(ioObj["active_state"].toBool() ? "高电平" : "低电平");
                        stateLabel->setStyleSheet(ioObj["active_state"].toBool() ?
                            "color: #27ae60;" : "color: #e74c3c;");
                        gridLayout->addWidget(stateLabel, row, 5);
                    }

                    row++;
                }
            } else if (type == "adc") {
                QJsonObject adcObj = configValue.toObject();
                QJsonArray channels = adcObj["channels"].toArray();

                gridLayout->addWidget(new QLabel("<b>通道数量:</b>"), row, 0);
                gridLayout->addWidget(new QLabel(QString::number(channels.size())), row, 1);
                row++;

                gridLayout->addWidget(new QLabel("<b>通道列表:</b>"), row, 0);

                QStringList channelList;
                for (const QJsonValue &ch : channels) {
                    channelList << ch.toString();
                }

                QLabel *channelsLabel = new QLabel(channelList.join(", "));
                channelsLabel->setWordWrap(true);
                gridLayout->addWidget(channelsLabel, row, 1, 1, 5);
                row++;
            } else if (type == "down_sop2") {
                QJsonObject sopObj = configValue.toObject();

                gridLayout->addWidget(new QLabel("<b>设备:</b>"), row, 0);
                gridLayout->addWidget(new QLabel(sopObj["device"].toString()), row, 1);
                row++;

                gridLayout->addWidget(new QLabel("<b>输出引脚:</b>"), row, 0);
                gridLayout->addWidget(new QLabel(sopObj["output_pin"].toString()), row, 1);
                row++;
            } else if (type == "down_boot") {
                QJsonObject bootObj = configValue.toObject();

                gridLayout->addWidget(new QLabel("<b>设备:</b>"), row, 0);
                gridLayout->addWidget(new QLabel(bootObj["device"].toString()), row, 1);
                row++;

                gridLayout->addWidget(new QLabel("<b>输出引脚:</b>"), row, 0);
                gridLayout->addWidget(new QLabel(bootObj["output_pin"].toString()), row, 1);
                row++;

                // 控制引脚
                QLabel *ctrlTitle = new QLabel("<b>控制引脚配置:</b>");
                ctrlTitle->setStyleSheet("color: #2c3e50;");
                gridLayout->addWidget(ctrlTitle, row, 0, 1, 6);
                row++;

                for (const QJsonValue &ctrlVal : bootObj["control"].toArray()) {
                    QJsonObject ctrlObj = ctrlVal.toObject();

                    gridLayout->addWidget(new QLabel("引脚:"), row, 1);
                    gridLayout->addWidget(new QLabel(QString::number(ctrlObj["io_pin"].toInt())), row, 2);

                    gridLayout->addWidget(new QLabel("状态:"), row, 3);

                    QLabel *stateLabel = new QLabel(ctrlObj["active_state"].toBool() ? "高电平" : "低电平");
                    stateLabel->setStyleSheet(ctrlObj["active_state"].toBool() ?
                        "color: #27ae60;" : "color: #e74c3c;");
                    gridLayout->addWidget(stateLabel, row, 4);

                    row++;
                }
            } else {
                // 通用配置处理
                if (configValue.isObject()) {
                    QJsonObject obj = configValue.toObject();
                    for (auto it = obj.begin(); it != obj.end(); ++it) {
                        gridLayout->addWidget(new QLabel("<b>" + it.key() + ":</b>"), row, 0);
                        gridLayout->addWidget(new QLabel(it.value().toVariant().toString()), row, 1);
                        row++;
                    }
                } else if (configValue.isArray()) {
                    QJsonArray arr = configValue.toArray();
                    for (int i = 0; i < arr.size(); ++i) {
                        gridLayout->addWidget(new QLabel("<b>项目 " + QString::number(i+1) + ":</b>"), row, 0);
                        gridLayout->addWidget(new QLabel(arr[i].toVariant().toString()), row, 1);
                        row++;
                    }
                }
            }
        }

        cardLayout->addWidget(contentWidget);
        contentLayout->addWidget(card);
    }

    // 添加伸缩项
    contentLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
}

QJsonArray CardConfigView::getModifiedConfig() const
{
    return m_currentConfig;
}
